package cn.hsa.xxx.bookOrder.bo.impl;

import cn.hsa.xxx.bookOrder.bo.OrderBO;
import cn.hsa.xxx.bookOrder.dao.OrderDAO;
import cn.hsa.xxx.bookOrder.dto.OrderDTO;
import cn.hsa.xxx.bookOrder.dto.OrderDTOConverter;
import cn.hsa.xxx.bookOrder.entity.OrderDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Component
@Slf4j
public class OrderBOImpl implements OrderBO {

    @Resource
    OrderDAO orderDAO;

    @Autowired
    OrderDTOConverter orderDTOConverter;

    @Override
    public OrderDTO add(OrderDTO orderDTO) {
        OrderDO orderDO = orderDTOConverter.convertToOrderDO(orderDTO);
        log.error("orderDO:{}",orderDO);
        orderDAO.insert(orderDO);
        return orderDTOConverter.convertToOrderDTO(orderDO);
    }

    @Override
    public String delete(String id) {
        orderDAO.deleteById(id);
        return "删除成功";
    }

    @Override
    public OrderDTO get(String id) {
        OrderDO orderDO = orderDAO.selectById(id);
        OrderDTO orderDTO = orderDTOConverter.convertToOrderDTO(orderDO);
        return orderDTO;
    }

    @Override
    public String update(OrderDTO orderDTO) {
        OrderDO orderDO = orderDTOConverter.convertToOrderDO(orderDTO);
        orderDAO.updateById(orderDO);
        return "更新成功";
    }

    @Override
    public List<OrderDTO> getPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<OrderDO> orderDOList = orderDAO.selectList(null);

        PageInfo pageInfo = new PageInfo(orderDOList);
        long totalRecords = pageInfo.getTotal();
        long totalPages = pageInfo.getPages();

        List<OrderDTO> orderDTOList = new ArrayList<>();
        for (OrderDO orderDO : orderDOList) {
            orderDTOList.add(orderDTOConverter.convertToOrderDTO(orderDO));
        }

        return orderDTOList;
    }
}
